package com.doliere.android.app.p2p.sockets;

import java.io.IOException;
import java.net.ServerSocket;

import com.doliere.android.app.p2p.services.P2pService;

import android.os.AsyncTask;

public class P2pServerSide extends AsyncTask<String, String, ServerSocket>{
	
	ServerSocket mServerSocket; // Socket for listening to entring connections.
	int mServerPort; // Get an available port on which to listen to entring connections.
	
	public P2pServerSide(P2pService service) throws IOException{
		mServerSocket = null;
		mServerPort = 0;
	}
	
	public void initializeServerSocket(){
		try {
			mServerSocket = new ServerSocket(0); // Get a free port
			mServerPort = mServerSocket.getLocalPort();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	
	public ServerSocket getServerSocket(){
		return mServerSocket;
	}
	
	public int getServerPort(){
		return mServerPort;
	}

	@Override
	protected ServerSocket doInBackground(String... params) {
		
		ServerSocket serverSocket = null;
		
		try {
			serverSocket = new ServerSocket(0); // Ask for an available free port.
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return serverSocket;
	}
	
	@Override
	protected void onPostExecute(ServerSocket server){
	
	}
}
